/** model */
import i18n from '@src/locales';
import { uuid } from '@src/util/lang/string';
import Field from '@model/entity/Field';
import Node from '@src/modules/setting/task/taskTypeSetting/flow/model/const/node';

/* utils */
import { GenerateNodeParams } from "@src/modules/setting/task/taskTypeSetting/flow/model/entity/convert";
/** enum */
import { FlowEnum } from '@model/enum/FlowEnum';

/**
 * 生成节点
 * @param params
 */
export const generateNode = (params: GenerateNodeParams) => {
  const { node, nextData, insertNode,  genNodeParams = {}, fields } = params;
  const { id, type, title, icon, isSystem, isCommon } = insertNode;

  return Node.generateNewNode(type, {
    ...genNodeParams,
    id,
    title,
    icon,
    isCommon,
    isSystem,
    targetId: nextData?.id || '',
  });
};

  /** 工单自定义节点根据上一个节点的类型添加节点弹窗数据 */
  export const getAddNodePopoverTypeList = (preType: string, addNodeTypeList = [] )=> {
    if (['finish', 'cost', 'review'].includes(preType)) {
      addNodeTypeList = addNodeTypeList.filter((item: any) => item.type === 'builtIn') ;
    }
    if (['normal'].includes(preType)) {
      addNodeTypeList = addNodeTypeList.filter((item: any) => item.type !== 'builtIn');
    }

     addNodeTypeList.forEach((item: any) => {
      if (preType === 'start') {
        item.list = item?.list?.filter((e: any) => e.type !== FlowEnum.COST && e.type !== FlowEnum.REVIEW) || [];
      }
      if (preType === 'accept' && item.type === 'builtIn') {
        item.list = item?.list?.filter((e: any) => e.type === FlowEnum.START) || [];
      }
      if (['finish', 'cost', 'review'].includes(preType) && item.type === 'builtIn') {
        item.list = item?.list?.filter((e: any) => e.type !== FlowEnum.START) ||  [];
      }
      return item;
    });
    return addNodeTypeList
  }